<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>Knuth.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="Knuth code in Java">
<meta NAME="TITLE" CONTENT="Knuth code in Java">
<meta NAME="KEYWORDS" CONTENT="Knuth,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>Knuth.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "Knuth.java">Knuth.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac Knuth.java</span>
<span class="comment"> *  Execution:    java Knuth &lt; list.txt</span>
<span class="comment"> *  Dependencies: StdIn.java StdOut.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/11model/cards.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/11model/cardsUnicode.txt</span>
<span class="comment"> *  </span>
<span class="comment"> *  Reads in a list of strings and prints them in random order.</span>
<span class="comment"> *  The Knuth (or Fisher-Yates) shuffling algorithm guarantees</span>
<span class="comment"> *  to rearrange the elements in uniformly random order, under</span>
<span class="comment"> *  the assumption that Math.random() generates independent and</span>
<span class="comment"> *  uniformly distributed numbers between 0 and 1.</span>
<span class="comment"> *</span>
<span class="comment"> *  % more cards.txt</span>
<span class="comment"> *  2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC</span>
<span class="comment"> *  2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD</span>
<span class="comment"> *  2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH</span>
<span class="comment"> *  2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS</span>
<span class="comment"> *</span>
<span class="comment"> *  % java Knuth &lt; cards.txt</span>
<span class="comment"> *  6H</span>
<span class="comment"> *  9C</span>
<span class="comment"> *  8H</span>
<span class="comment"> *  7C</span>
<span class="comment"> *  JS</span>
<span class="comment"> *  ...</span>
<span class="comment"> *  KH</span>
<span class="comment"> *</span>
<span class="comment"> *  % more cardsUnicode.txt</span>
<span class="comment"> *  2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ 10♣ J♣ Q♣ K♣ A♣ </span>
<span class="comment"> *  2♦ 3♦ 4♦ 5♦ 6♦ 7♦ 8♦ 9♦ 10♦ J♦ Q♦ K♦ A♦ </span>
<span class="comment"> *  2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ 10♥ J♥ Q♥ K♥ A♥ </span>
<span class="comment"> *  2♠ 3♠ 4♠ 5♠ 6♠ 7♠ 8♠ 9♠ 10♠ J♠ Q♠ K♠ A♠ </span>
<span class="comment"> * </span>
<span class="comment"> *  % java Knuth &lt; cardsUnicode.txt</span>
<span class="comment"> *  2♠</span>
<span class="comment"> *  K♥</span>
<span class="comment"> *  6♥</span>
<span class="comment"> *  5♣</span>
<span class="comment"> *  J♣</span>
<span class="comment"> *  ...</span>
<span class="comment"> *  A♦</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Knuth</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides a client for reading in a </span>
<span class="comment"> *  sequence of strings and </span><span class="keyword">&lt;em&gt;</span><span class="comment">shuffling</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> them using the Knuth (or Fisher-Yates)</span>
<span class="comment"> *  shuffling algorithm. This algorithm guarantees to rearrange the</span>
<span class="comment"> *  elements in uniformly random order, under</span>
<span class="comment"> *  the assumption that Math.random() generates independent and</span>
<span class="comment"> *  uniformly distributed numbers between 0 and 1.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/11model"</span><span class="keyword">&gt;</span><span class="comment">Section 1.1</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *  See {</span><span class="type">@link</span><span class="comment"> StdRandom} for versions that shuffle arrays and</span>
<span class="comment"> *  subarrays of objects, doubles, and ints.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Knuth</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span>

<span class="normal">    </span><span class="comment">// this class should not be instantiated</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">Knuth</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rearranges an array of objects in uniformly random order</span>
<span class="comment">     * (under the assumption that </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Math.random()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> generates independent</span>
<span class="comment">     * and uniformly distributed numbers between 0 and 1).</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> a the array to be shuffled</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">Object</span><span class="symbol">[]</span><span class="normal"> a</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">// choose index uniformly in [i, N-1]</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">random</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">N </span><span class="symbol">-</span><span class="normal"> i</span><span class="symbol">));</span>
<span class="normal">            </span><span class="usertype">Object</span><span class="normal"> swap </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> swap</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads in a sequence of strings from standard input, shuffles</span>
<span class="comment">     * them, and prints out the results.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// read in the data</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> a </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readAllStrings</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// shuffle the array</span>
<span class="normal">        Knuth</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// print results.</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
